package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.config.RabbitMQConfig;
import com.itheima.dao.BlogMapper;
import com.itheima.dao.BlogSortMapper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.TagMapper;
import com.itheima.pojo.*;
import com.itheima.service.BlogService;
import com.itheima.utils.IdWorker;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import java.text.SimpleDateFormat;
import java.util.*;



@Service
public class BlogServiceImpl implements BlogService {


    @Autowired
    private BlogMapper blogMapper;

     @Autowired
     private TagMapper tagMapper;

     @Autowired
     private BlogSortMapper blogSortMapper;

     @Autowired
     private FileMapper fileMapper;


     @Autowired
     private IdWorker idWorker;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;




     //1. 添加博客
    @Override
    public void add(BlogVO blogVO) {

        //分装blog对象
        Blog blog = new Blog();

        blog.setAdminUid(blogVO.getAdminUid());
        blog.setArticlesPart(blogVO.getArticlesPart());
        blog.setAuthor(blogVO.getAuthor());
        blog.setBlogSortUid(blogVO.getBlogSortUid());
        blog.setContent(blogVO.getContent());
        blog.setFileUid(blogVO.getFileUid());
        blog.setIsOriginal(blogVO.getIsOriginal());
        blog.setIsPublish(blogVO.getIsPublish());
        blog.setLevel(blogVO.getLevel());
        blog.setSort(blogVO.getSort());
        blog.setStartComment(Integer.valueOf(blogVO.getStartComment()));
        blog.setStatus(1);
        blog.setSummary(blogVO.getSummary());
        blog.setTagUid(blogVO.getTagUid());
        blog.setTitle(blogVO.getTitle());
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        String uid = idWorker.nextId()+"";
        blog.setUid(uid);

        BlogSort blogSort = blogVO.getBlogSort();
        blogSort.setCreateTime(new Date());
        blogSort.setUpdateTime(new Date());
        blogSort.setUid(idWorker.nextId()+"");
        blogSort.setSort(1);
        List<String> photoList = blogVO.getPhotoList();
        List<Tag> tagList = blogVO.getTagList();

        //存入数据到数据库

        blogMapper.insert(blog);
        blogSortMapper.insert(blogSort);

        for (Tag tag : tagList) {
            tag.setCreateTime(new Date());
            tag.setUpdateTime(new Date());
            tag.setUid(idWorker.nextId()+"");
            tagMapper.insert(tag);
        }

        for (String picUrl : photoList) {
            File file = new File();
            file.setPicUrl(picUrl);
            file.setCreateTime(new Date());
            file.setUpdateTime(new Date());
            String uid2 = idWorker.nextId()+"";
            file.setUid(uid2);
            file.setStatus(1);
            fileMapper.insert(file);
        }
        //将uid添加队列
        rabbitTemplate.convertAndSend(RabbitMQConfig.ADD_EXCHANGE,"",blog.getUid());
    }

   //2.删除博客
    @Override
    public void delete(BlogVO blogVO){

        String uid = blogVO.getUid();
        blogMapper.deleteByPrimaryKey(uid);

        //将uid添加队列
        rabbitTemplate.convertAndSend(RabbitMQConfig.DELETE_EXCHANGE,"",uid);

    }

    //3.删除选中博客
    @Override
    public void deleteBatch(List<BlogVO> list) {

        for (BlogVO blogVO : list){

            String uid = blogVO.getUid();
            blogMapper.deleteByPrimaryKey(uid);

            //将uid添加队列
            rabbitTemplate.convertAndSend(RabbitMQConfig.DELETE_EXCHANGE,"",uid);
        }
    }

    //4.编辑博客
    @Override
    public void edit(BlogVO blogVO) {

        //分装blog对象 2.再添加
        Blog blog2 = new Blog();

        blog2.setArticlesPart(blogVO.getArticlesPart());
        blog2.setAuthor(blogVO.getAuthor());
        blog2.setBlogSortUid(blogVO.getBlogSortUid());
        blog2.setClickCount(blogVO.getClickCount());
        blog2.setCollectCount(blogVO.getCollectCount());

        blog2.setContent(blogVO.getContent());
        blog2.setCreateTime(new Date());
        blog2.setIsOriginal(blogVO.getIsOriginal());
        blog2.setIsPublish(blogVO.getIsPublish());
        blog2.setLevel(blogVO.getLevel());

        blog2.setSort(blogVO.getSort());
        blog2.setStartComment(Integer.parseInt(blogVO.getStartComment()));

        blog2.setStatus(blogVO.getStatus());
        blog2.setSummary(blogVO.getSummary());

        blog2.setTagUid(blogVO.getTagUid());
        blog2.setTitle(blogVO.getTitle());

        blog2.setUid( blogVO.getUid());
        blog2.setUpdateTime(new Date());

        //修改博客数据
        blogMapper.updateByPrimaryKeySelective(blog2);

    }

    @Override
    public PageInfo<Blog> getList(BlogVO blogVO) {

        PageHelper.startPage( blogVO.getCurrentPage(),blogVO.getPageSize());
        Example example = createExample(blogVO);
        List<Blog> blogList = blogMapper.selectByExample(example);
        for (Blog blog : blogList) {

            //封装标签集合数据
            String tagUid = blog.getTagUid();
            Tag tag = new Tag();
            tag.setUid(tagUid);
            List<Tag> tagList = tagMapper.select(tag);
            blog.setTagList(tagList);

            //封装博客分类对象
            String blogSortUid = blog.getBlogSortUid();
            BlogSort blogSort = new BlogSort();
            blogSort.setUid(blogSortUid);
            BlogSort blogSort1 = blogSortMapper.selectOne(blogSort);
            blog.setBlogSort(blogSort1);

            //封装photoList数据
            String fileUid = blog.getFileUid();
            String url = fileMapper.getPicUrlByUid(fileUid);
            List<String> photoList= new ArrayList<>();
            photoList.add(url);
            blog.setPhotoList(photoList);
        }
           //返回结果
           return new PageInfo<>(blogList);
    }

    /**
     * 查询所有已发布播客
     * @return
     */
    @Override
    public List<Blog> findList() {
     return blogMapper.findList();
    }

    /**
     * 根据uid查询对象
     * @param uid
     * @return
     */
    @Override
    public Blog findBlogByUid(String uid) {
       return blogMapper.findBlogByUid(uid);
    }


    /**
     * 根据blogSortUid查询blogList
     * @param blogSortUid
     * @return
     */
    @Override
    public PageInfo getBlogListBySortUid(String blogSortUid,Integer currentPage,Integer pageSize) {
        List<Blog> blogList = blogMapper.findBlogListBySortUid(blogSortUid);
        PageHelper.startPage(currentPage,pageSize);
        return  new PageInfo<>(blogList);
    }

    /**
     * 根据等级和排序字段查询列表
     * @param level
     * @param sort
     * @return
     */
    @Override
    public PageInfo findByLevelAndSort(Integer level,Integer sort,Long currentPage) {
        List<Blog> blogList = blogMapper.findByLevelAndSort(level, sort);
        PageHelper.startPage(Math.toIntExact(currentPage),10);
        return new PageInfo<>(blogList);
    }

    /**
     * 根据时间查询播客
     * @param
     * @param
     * @return
     */
    @Override
    public PageInfo findByTime(Integer currentPage,Integer pageSize) {
        //获取查询的时间段
        Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
        Date endTime = calendar.getTime();
        calendar.add(Calendar.MONTH,-6);//获得当前时间往前推12个月的时间
        Date starTime = calendar.getTime();
        List<Blog> blogList = blogMapper.findByTime(starTime, endTime);
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo<>(blogList);
    }

    /**
     * 根据点击数查询前5名
     * @return
     */
    @Override
    public List<Blog> findHotBlog() {
        return blogMapper.findHotBlog();
    }

    /**
     * 按时间排序查询
     * @return
     */
    @Override
    public PageInfo<Blog> findNewBlog(Integer currentPage,Integer pageSize) {
        List<Blog> blogList = blogMapper.findNewBlog();
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo<>(blogList);
    }



    // (tagUid,currentPage,pageSize)通过TagUid获取文章
    @Override
    public PageInfo<Blog> getArticleByTagUid(String tagUid, String currentPage, String pageSize) {
        PageHelper.startPage( Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        List<Blog> blogList = blogMapper.getArticleByTagUid(tagUid);
        for (Blog blog : blogList) {

            //封装标签集合数据

            Tag tag = new Tag();
            tag.setUid(tagUid);
            List<Tag> tagList = tagMapper.select(tag);
            blog.setTagList(tagList);

            //封装博客分类对象
            String blogSortUid = blog.getBlogSortUid();
            BlogSort blogSort = new BlogSort();
            blogSort.setUid(blogSortUid);
            BlogSort blogSort1 = blogSortMapper.selectOne(blogSort);
            blog.setBlogSort(blogSort1);

            //封装photoList数据
            String fileUid = blog.getFileUid();
            String url = fileMapper.getPicUrlByUid(fileUid);
            List<String> photoList= new ArrayList<>();
            photoList.add(url);
            blog.setPhotoList(photoList);
        }

       return new PageInfo<>(blogList);
    }

    //通过月份获取文章 2020年03月
    @Override
    public  PageInfo<Blog> getArticleByMonth(String monthDate) {

        String replace = monthDate.replace("年", "-");
        String month = replace.replace("月", "");
        System.out.println(month);
        List<Blog> blogList = blogMapper.getArticleByMonth(month);
        for (Blog blog : blogList) {

            //封装标签集合数据
            Tag tag = new Tag();
            tag.setUid(blog.getTagUid());
            List<Tag> tagList = tagMapper.select(tag);
            blog.setTagList(tagList);

            //封装博客分类对象
            String blogSortUid = blog.getBlogSortUid();
            BlogSort blogSort = new BlogSort();
            blogSort.setUid(blogSortUid);
            BlogSort blogSort1 = blogSortMapper.selectOne(blogSort);
            blog.setBlogSort(blogSort1);
        }

       return new PageInfo<>(blogList);
    }

    @Override
    public List<String> getSortList() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);

        ArrayList<String> list = new ArrayList<>();

        int sortList1 = blogMapper.getSortList1(year);
        if (sortList1>0){
            list.add("2020年01月");
        }
        int sortList2 = blogMapper.getSortList2(year);
        if (sortList2>0){
            list.add("2020年02月");
        }
        int sortList3 = blogMapper.getSortList3(year);
        if (sortList3>0){
            list.add("2020年03月");
        }
        int sortList4 = blogMapper.getSortList4(year);
        if (sortList4>0){
            list.add("2020年04月");
        }

        int sortList5 = blogMapper.getSortList5(year);
        if (sortList5>0){
            list.add("2020年05月");
        }
        int sortList6 = blogMapper.getSortList6(year);
        if (sortList6>0){
            list.add("2020年06月");
        }
        int sortList7 = blogMapper.getSortList7(year);
        if (sortList7>0){
            list.add("2020年07月");
        }
        int sortList8 = blogMapper.getSortList8(year);
        if (sortList8>0){
            list.add("2020年08月");
        }

        int sortList9 = blogMapper.getSortList9(year);
        if (sortList9>0){
            list.add("2020年09月");
        }
        int sortList10 = blogMapper.getSortList10(year);
        if (sortList10>0){
            list.add("2020年10月");
        }

        int sortList11 = blogMapper.getSortList11(year);
        if (sortList11>0){
            list.add("2020年11月");
        }
        int sortList12 = blogMapper.getSortList12(year);
        if (sortList12>0){
            list.add("2020年12月");
        }
        return list;
    }


    /*
     * 封装查询条件对象*/
    private Example createExample(BlogVO blogVO) {
        Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        if (blogVO!= null) {
            // 根据博客名称模糊查询
            if(blogVO.getKeyword()!=null && !"".equals(blogVO.getKeyword())){
                criteria.andLike("title","%"+blogVO.getKeyword()+"%");
            }

            // 根据博客分类名称查询
            if(blogVO.getBlogSortUid()!=null && !"".equals(blogVO.getBlogSortUid())){
                criteria.andEqualTo("blogSortUid",blogVO.getBlogSortUid());
            }

            // 根据博客标签名称查询
            String tagUid = blogVO.getTagUid();
            if( tagUid !=null && !"".equals( tagUid)){
                    criteria.andEqualTo("tagUid",tagUid);
            }
        }
        return example;
    }


    /**
     * 根据博客uid点赞
     * @param uid
     * @return
     */
    public String praiseBlogByUid(String uid){
        String praiseCount = (String) redisTemplate.opsForValue().get(BLOG_PRAISE_ + uid);
        //如果redis里面没有数据,就把点赞设为1
        if(StringUtils.isEmpty(praiseCount)){
            redisTemplate.opsForValue().set(BLOG_PRAISE_+uid,"1");
        }
        //如果有数据就把redis里面的点赞数+1
        praiseCount = String.valueOf(Integer.parseInt(praiseCount)+1);
        redisTemplate.opsForValue().set(BLOG_PRAISE_+uid,praiseCount);
        HashMap map = new HashMap();
        map.put("code","success");
        map.put("data","点赞成功");
        return JSON.toJSONString(map);
    }

    /**
     *根据标签获取相关的博客
     */
    public PageInfo getSameBlogByTagUid(String tagUid, Long currentPage, Long pageSize){
        Integer start = Integer.parseInt(currentPage.toString());
        Integer end = Integer.parseInt(pageSize.toString());
        PageHelper.startPage(start,end);
        //根据标签id查询博客
        List<Blog> blogByTagUid = blogMapper.findBlogByTagUid(tagUid);

        List<Map> mapList = new ArrayList<>();
        for (Blog blog : blogByTagUid) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Map map = new HashMap();
            map.put("title",blog.getTitle());
            map.put("summary",blog.getSummary());
            map.put("content",blog.getContent());
            map.put("tagUid",blog.getTagUid());
            map.put("blogSortUid",blog.getBlogSortUid());
            map.put("clickCount",blog.getClickCount());
            map.put("collectCount",blog.getCollectCount());
            map.put("fileUid",blog.getFileUid());
            map.put("adminUid",blog.getAdminUid());
            map.put("isPublish",blog.getIsPublish());
            map.put("isOriginal",blog.getIsOriginal());
            map.put("author",blog.getAuthor());
            map.put("articlesPart",blog.getArticlesPart());
            map.put("level",blog.getLevel());
            map.put("sort",blog.getSort());
            map.put("startComment",blog.getStartComment());
            map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
            map.put("uid",blog.getUid());
            map.put("status",blog.getStatus());
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("createTime",simpleDateFormat.format(blog.getCreateTime()));
            map.put("updateTime",simpleDateFormat.format(blog.getUpdateTime()));
            mapList.add(map);
        }
        return new PageInfo(mapList);
    }





    /**
     * 通过Uid获取博客内容
     * @param uid
     * @return
     */
    @Override
    public String getBlogByUid(String uid) {
        Blog blog = blogMapper.selectByPrimaryKey(uid);
        if(blog==null||blog.getIsPublish()=="0"){
            Map map = new HashMap();
            map.put("code",400);
            map.put("data","该文章已下架或被删除");
            return JSON.toJSONString(map);
        }
        HashMap map = new HashMap();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        map.put("title",blog.getTitle());
        map.put("summary",blog.getSummary());
        map.put("content",blog.getContent());
        map.put("tagUid",blog.getTagUid());
        map.put("blogSortUid",blog.getBlogSortUid());
        map.put("clickCount",blog.getClickCount());
        map.put("collectCount",blog.getCollectCount());
        map.put("fileUid",blog.getFileUid());
        map.put("adminUid",blog.getAdminUid());
        map.put("isPublish",blog.getIsPublish());
        map.put("isOriginal",blog.getIsOriginal());
        map.put("author",blog.getAuthor());
        map.put("articlesPart",blog.getArticlesPart());
        map.put("level",blog.getLevel());
        map.put("sort",blog.getSort());
        map.put("startComment",blog.getStartComment());
        map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
        map.put("uid",blog.getUid());
        map.put("status",blog.getStatus());
        map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
        map.put("createTime",simpleDateFormat.format(blog.getCreateTime()));
        map.put("updateTime",simpleDateFormat.format(blog.getUpdateTime()));
        HashMap resultMap = new HashMap();
        resultMap.put("code",200);
        resultMap.put("data",map);
        return JSON.toJSONString(resultMap);
    }

    //存在redis的键名
    private static  final String BLOG_PRAISE_ = "blog_praise_";

    /**
     * 通过Uid获取博客点赞数
     * @param uid
     * @return
     */

    @Override
    public String getBlogPraiseCountByUid(String uid) {
        Integer count = 0;
        if(StringUtils.isEmpty(uid)){
            return count.toString();
        }
        //从redis中获取点赞数
        String  priseCount= (String) redisTemplate.opsForValue().get(BLOG_PRAISE_ + uid);
        if(!StringUtils.isEmpty(priseCount)){
            count=Integer.parseInt(priseCount);
        }
        Map map = new HashMap();
        map.put("code","success");
        map.put("data",count.toString());
        return JSON.toJSONString(map);
    }

    /**
     * 根据BlogUid获取同类型的博客
     * @param uid
     * @return
     */
    @Override
    public PageInfo getSameBlogByBlogUid(String uid) {
        List<Blog> blogList = blogMapper.getSameBlogByBlogUid(uid);
        List<Map> mapList = new ArrayList<>();
        for (Blog blog : blogList) {
            Map map = new HashMap();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            map.put("title", blog.getTitle());
            map.put("summary", blog.getSummary());
            map.put("content", blog.getContent());
            map.put("tagUid", blog.getTagUid());
            map.put("blogSortUid", blog.getBlogSortUid());
            map.put("clickCount", blog.getClickCount());
            map.put("collectCount", blog.getCollectCount());
            map.put("fileUid", blog.getFileUid());
            map.put("adminUid", blog.getAdminUid());
            map.put("isPublish", blog.getIsPublish());
            map.put("isOriginal", blog.getIsOriginal());
            map.put("author", blog.getAuthor());
            map.put("articlesPart", blog.getArticlesPart());
            map.put("level", blog.getLevel());
            map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("startComment", blog.getStartComment());
            map.put("uid", blog.getUid());
            map.put("status", blog.getStatus());
            map.put("blogSort", this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("createTime", simpleDateFormat.format(blog.getCreateTime()));
            map.put("updateTime", simpleDateFormat.format(blog.getUpdateTime()));
            mapList.add(map);
        }
        return new PageInfo(mapList);
    }

    /**
     *根据作者获取相关博客
     * @param author
     * 搜索Blog
     * @param
     * @param currentPage
     * @param pageSize
     * @return
     */

    public PageInfo searchBlogByAuthor(String author,Long currentPage,Long pageSize){
        //用分页插件进行分页
        int start = Integer.parseInt(currentPage.toString());
        int end = Integer.parseInt(pageSize.toString());
        PageHelper.startPage(start,end);

        //根据作者获取相关博客
        List<Blog> blogList = blogMapper.searchBlogByAuthor(author);
        List<Map> mapList = new ArrayList<>();

        for (Blog blog : blogList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            HashMap map = new HashMap();
            map.put("title",blog.getTitle());
            map.put("summary",blog.getSummary());
            map.put("content",blog.getContent());
            map.put("tagUid",blog.getTagUid());
            map.put("blogSortUid",blog.getBlogSortUid());
            map.put("clickCount",blog.getClickCount());
            map.put("collectCount",blog.getCollectCount());
            map.put("fileUid",blog.getFileUid());
            map.put("adminUid",blog.getAdminUid());
            map.put("isPublish",blog.getIsPublish());
            map.put("isOriginal",blog.getIsOriginal());
            map.put("author",blog.getAuthor());
            map.put("articlesPart",blog.getArticlesPart());
            map.put("level",blog.getLevel());
            map.put("startComment",blog.getStartComment());
            map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("photoList",this.getPicUrlByUid(blog.getFileUid()));
            map.put("photoUrl",fileMapper.getPicUrlByUid(blog.getFileUid()));
            map.put("uid",blog.getUid());
            map.put("status",blog.getStatus());
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("createTime",simpleDateFormat.format(blog.getCreateTime()));
            map.put("updateTime",simpleDateFormat.format(blog.getUpdateTime()));
            mapList.add(map);
        }
        PageInfo pageInfo = new PageInfo(mapList);
        return pageInfo;
    }


    /**
     * 根据分类id获取相关的博客
     * @param blogSortUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo searchBlogBySort(String blogSortUid, Long currentPage, Long pageSize) {
        //用分页插件进行分页
        int start = Integer.parseInt(currentPage.toString());
        int end = Integer.parseInt(pageSize.toString());
        PageHelper.startPage(start,end);
        List<Blog> blogList = blogMapper.findBlogByBlogSortUid(blogSortUid);
        List<Map> mapList = new ArrayList<>();
        for (Blog blog : blogList) {
            HashMap map = new HashMap();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            map.put("title",blog.getTitle());
            map.put("summary",blog.getSummary());
            map.put("content",blog.getContent());
            map.put("tagUid",blog.getTagUid());
            map.put("blogSortUid",blog.getBlogSortUid());
            map.put("clickCount",blog.getClickCount());
            map.put("collectCount",blog.getCollectCount());
            map.put("fileUid",blog.getFileUid());
            map.put("adminUid",blog.getAdminUid());
            map.put("isPublish",blog.getIsPublish());
            map.put("isOriginal",blog.getIsOriginal());
            map.put("author",blog.getAuthor());
            map.put("articlesPart",blog.getArticlesPart());
            map.put("level",blog.getLevel());
            map.put("startComment",blog.getStartComment());
            map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("photoList",this.getPicUrlByUid(blog.getFileUid()));
            map.put("photoUrl",fileMapper.getPicUrlByUid(blog.getFileUid()));
            map.put("uid",blog.getUid());
            map.put("status",blog.getStatus());
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("createTime",simpleDateFormat.format(blog.getCreateTime()));
            map.put("updateTime",simpleDateFormat.format(blog.getUpdateTime()));
            mapList.add(map);
        }
        PageInfo pageInfo = new PageInfo(mapList);
        return pageInfo;
    }

    /**
     * 根据标签获取相关的博客
     * @param tagUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo searchBlogByTag(String tagUid, Long currentPage, Long pageSize) {
        //用分页插件进行分页
        int start = Integer.parseInt(currentPage.toString());
        int end = Integer.parseInt(pageSize.toString());
        PageHelper.startPage(start,end);
        List<Blog> blogList = blogMapper.findBlogByTagUid(tagUid);
        List<Map> mapList = new ArrayList<>();
        for (Blog blog : blogList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            HashMap map = new HashMap();
            map.put("title",blog.getTitle());
            map.put("summary",blog.getSummary());
            map.put("content",blog.getContent());
            map.put("tagUid",blog.getTagUid());
            map.put("blogSortUid",blog.getBlogSortUid());
            map.put("clickCount",blog.getClickCount());
            map.put("collectCount",blog.getCollectCount());
            map.put("fileUid",blog.getFileUid());
            map.put("adminUid",blog.getAdminUid());
            map.put("isPublish",blog.getIsPublish());
            map.put("isOriginal",blog.getIsOriginal());
            map.put("author",blog.getAuthor());
            map.put("articlesPart",blog.getArticlesPart());
            map.put("level",blog.getLevel());
            map.put("startComment",blog.getStartComment());
            map.put("tagList",this.getBlogTagByUid(blog.getTagUid()));
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("photoList",this.getPicUrlByUid(blog.getFileUid()));
            map.put("photoUrl",fileMapper.getPicUrlByUid(blog.getFileUid()));
            map.put("uid",blog.getUid());
            map.put("status",blog.getStatus());
            map.put("blogSort",this.getBlogSortByUid(blog.getBlogSortUid()));
            map.put("createTime",simpleDateFormat.format(blog.getCreateTime()));
            map.put("updateTime",simpleDateFormat.format(blog.getUpdateTime()));
            mapList.add(map);
        }
        PageInfo pageInfo = new PageInfo(mapList);
        return pageInfo;
    }


    //根据标签id获取博客
    private List<Map> getBlogTagByUid(String uid){
        Tag tag = tagMapper.selectByPrimaryKey(uid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("content",tag.getContent());
        map.put("clickCount",tag.getClickCount());
        map.put("sort",tag.getSort());
        map.put("uid",tag.getUid());
        map.put("status",tag.getStatus());
        map.put("createTime",simpleDateFormat.format(tag.getCreateTime()));
        map.put("updateTime",simpleDateFormat.format(tag.getUpdateTime()));
        list.add(map);
        return list;
    }

    //根据分类id获取博客
    private Map getBlogSortByUid(String uid){
        BlogSort blogSort = blogSortMapper.selectByPrimaryKey(uid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Map map = new HashMap();
        map.put("sortName",blogSort.getSortName());
        map.put("content",blogSort.getContent());
        map.put("clickCount",blogSort.getClickCount());
        map.put("sort",blogSort.getSort());
        map.put("uid",blogSort.getUid());
        map.put("status",blogSort.getStatus());
        map.put("createTime",simpleDateFormat.format(blogSort.getCreateTime()));
        map.put("updateTime",simpleDateFormat.format(blogSort.getUpdateTime()));

        return map;
    }

    //根据fileUid获取图片路径
    private List<String> getPicUrlByUid(String uid){
        String picUrlByUid = fileMapper.getPicUrlByUid(uid);
        List list = new ArrayList();
        list.add(picUrlByUid);
        return list;
    }

    /**
     * 根据关键字查询
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo sqlSearchBlog(String keywords, Integer currentPage, Integer pageSize) {
        List<Blog> blogList = blogMapper.sqlSearchBlog(keywords);
        PageHelper.startPage(currentPage,pageSize);
        PageInfo pageInfo=new PageInfo(blogList);
        return pageInfo;
    }

}
